package com.qianya.oa.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianya.oa.common.annotation.Log;
import com.qianya.oa.common.constant.UserConstants;
import com.qianya.oa.common.core.controller.BaseController;
import com.qianya.oa.common.core.domain.AjaxResult;
import com.qianya.oa.common.core.page.TableDataInfo;
import com.qianya.oa.common.enums.BusinessType;
import com.qianya.oa.common.utils.poi.ExcelUtil;
import com.qianya.oa.domain.SysPost;
import com.qianya.oa.service.SysPostService;
import com.qianya.oa.util.CommonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 * 
 * @author qianya
 */
@Controller
@RequestMapping("/system/post")
public class SysPostController extends BaseController
{
    private String prefix = "system/post";

    @Autowired
    private SysPostService postService;

    @RequiresPermissions("system:post:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/post";
    }

    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysPost post)
    {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysPost post)
    {
        List<SysPost> list = postService.selectPostList(post);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        return util.exportExcel(list, "岗位数据");
    }

    @RequiresPermissions("system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(postService.deletePostByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysPost post)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getLoginName());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap mmap) {
        mmap.put("post", postService.selectPostById(postId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysPost post)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getLoginName());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(SysPost post)
    {
        return postService.checkPostNameUnique(post);
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(SysPost post)
    {
        return postService.checkPostCodeUnique(post);
    }

//-----------------新版分割线--------------------------
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    @PostMapping("/getPostList")
    @ResponseBody
    public CommonResult<?> getPostList(@RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize,@RequestParam(value = "postName",required = false) String postName){
        PageHelper.startPage(pageNum,pageSize);
        SysPost sysPost = new SysPost();
        sysPost.setPostName(postName);
        List<SysPost> list = postService.selectPostList(sysPost);
        PageInfo<SysPost> pageInfo = new PageInfo<>(list);
        return CommonResult.success(pageInfo);
    }

    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    @Log(title = "删除岗位", businessType = BusinessType.DELETE)
    @PostMapping("/removePostById")
    @ResponseBody
    public CommonResult<?> removePostById(@RequestParam("id") String id) {
        try {
            return CommonResult.success(postService.deletePostByIds(id));
        }
        catch (Exception e) {
            return CommonResult.erro(e.getMessage());
        }
    }
    /**
     * 获取岗位信息
     */
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    @GetMapping("/getPostInfoById")
    @ResponseBody
    public CommonResult<?> getPostInfoById(@RequestParam("postId") Long postId) {
        return CommonResult.success(postService.selectPostById(postId));
    }

    /**
     * 修改保存
     */
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    @Log(title = "修改岗位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePostInfo")
    @ResponseBody
    public CommonResult<?> updatePostInfo(@RequestBody @Validated SysPost post)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return CommonResult.erro("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return CommonResult.erro("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getLoginName());
        return CommonResult.success(postService.updatePost(post));
    }

    /**
     * 新增保存岗位
     */

    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    @PostMapping("/addPost")
    @ResponseBody
    public CommonResult<?> addPost(@RequestBody @Validated SysPost post)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return CommonResult.erro("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.POST_CODE_NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return CommonResult.erro("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getLoginName());
        return CommonResult.success(postService.insertPost(post));
    }


}
